package org.devocean.groupbuying.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.devocean.groupbuying.dao.TagsDAO;
import org.devocean.groupbuying.domain.Tags;
import org.springframework.stereotype.Repository;

@Repository
public class TagsDAOImpl implements TagsDAO {

	@PersistenceContext
	private EntityManager em;
	
	public List<Tags> findAllTags() {
		
		return em.createQuery("select t from Tags t",Tags.class).getResultList() ;
		
	}

	public List<Tags> findTagsByCriteria(String criteria,Integer firstResult,Integer maxResults) {
		try{
			return em.createQuery("select t from Tags t where t.title like :criteria", Tags.class).setParameter("criteria",criteria+"%")
					.setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
		}catch (Exception e) {
			System.out.println(e.getMessage());
		}
		return null;
		
		 
	}

	public long countTagsByCriteria(String criteria) {
		
		return em.createQuery("select COUNT(t) from Tags t where t.title like :criteria",Long.class).setParameter("criteria",criteria+"%").getSingleResult();
	}

}
